package com.qianji.controller;

import com.qianji.pojo.QRCodeDto;
import com.qianji.service.QrCodeService;
import com.qianji.utils.Result;
import com.qianji.vo.QrCodeAddVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import static com.alibaba.excel.util.StringUtils.isEmpty;

/**
 * @Author 钟丽诗
 * @Date 2021.3.29
 */

@Slf4j
@RestController
@RequestMapping("/qrCode/")
public class QrCodeController {

    @Autowired
    private QrCodeService qrCodeService;

    @ApiOperation("新增二维码链接")
    @RequestMapping(value = "/qrCodeAdd",method = RequestMethod.POST)
    public Result qrCodeAdd(QrCodeAddVO qrCodeAddVO){
        if (isEmpty(qrCodeAddVO)){
            return Result.failed("传入的参数为空，请传入您要新增的信息！！！",500);
        }
        qrCodeAddVO.setCreateTime(new Date());
        log.info("新增二维码链接入参：{}",qrCodeAddVO);
        Integer status = qrCodeService.qrCodeAdd(qrCodeAddVO);
        if(status != 0){
            return Result.success("新增成功！",200);
        }else {
            return Result.failed("新增失败，二维码名称重复！！！",500);
        }

    }

    @ApiOperation("修改二维码链接信息")
    @RequestMapping(value = "/qrCodeUpdate",method = RequestMethod.POST)
    public Result qrCodeUpdate(QRCodeDto qrCodeDto){
        if (isEmpty(qrCodeDto)){
            return Result.failed("传入的参数为空，请传入您要修改的信息！！！",500);
        }
        qrCodeDto.setUpdateTime(new Date());
        log.info("修改二维码链接入参：{}",qrCodeDto);
        Integer status = qrCodeService.qrCodeUpdate(qrCodeDto);
        if(status == 1){
            return Result.success("修改成功",200);
        }else{
            return Result.failed("修改失败",500);
        }
    }

    @ApiOperation("根据二维码链接名称模糊查询")
    @RequestMapping(value = "/findQrCode",method = RequestMethod.POST)
    public Result findQrCode(String qrName){

        return Result.success(qrCodeService.findQrCode(qrName));

    }

    @ApiOperation("根据id查询")
    @RequestMapping(value = "/findById",method = RequestMethod.POST)
    public Result findById(Long id){

        return Result.success(qrCodeService.findById(id));

    }

}
